package cn.lcvc.xnc.application.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.lcvc.xnc.application.common.exception.model.MyWebException;
import cn.lcvc.xnc.application.dao.WasteDisposalSiteMapper;
import cn.lcvc.xnc.application.dao.WasteDisposalSitePatrolMapper;
import cn.lcvc.xnc.application.dao.WasteDisposalSitePatrolPictureMapper;
import cn.lcvc.xnc.application.model.*;
import cn.lcvc.xnc.application.model.query.WasteDisposalSitePatrolQuery;
import cn.lcvc.xnc.application.service.WasteDisposalSitePatrolService;
import cn.lcvc.xnc.authority.service.impl.IBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;


/**
 * 消纳场巡逻服务
 *
 * @author 廖振豪
 * @date 2022/08/08
 */
@Service
@Slf4j
public class IWasteDisposalSitePatrolService extends IBaseService<WasteDisposalSitePatrol, WasteDisposalSitePatrolQuery> implements WasteDisposalSitePatrolService {

    @Autowired
    private WasteDisposalSitePatrolMapper mapper;

    @Autowired
    private WasteDisposalSiteMapper wasteDisposalSiteMapper;

    @Autowired
    private WasteDisposalSitePatrolPictureMapper wasteDisposalSitePatrolPictureMapper;

    @Override
    protected void beforeGet(Serializable id) {
        if (mapper.findById(id) == null) {
            throw new MyWebException("查找失败：找不到指定记录");
        }
    }

    @Override
    protected void beforeUpdate(WasteDisposalSitePatrol wasteDisposalSitePatrol) throws MyWebException {
        if (wasteDisposalSitePatrol.getId() == null) {
            throw new MyWebException("编辑失败：id不能为空");
        }
        if (mapper.findById(wasteDisposalSitePatrol.getId()) == null) {
            throw new MyWebException("编辑失败：找不到指定记录");
        }
        wasteDisposalSitePatrolPictureMapper.deleteBySitePatrolId(wasteDisposalSitePatrol.getId());
    }

    @Override
    protected void beforeSave(WasteDisposalSitePatrol wasteDisposalSitePatrol) {
        if (wasteDisposalSiteMapper.findById(wasteDisposalSitePatrol.getSiteId()) ==null ){
            throw new MyWebException("添加失败：该消纳场不存在");
        }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
//        wasteDisposalSitePatrolPictureMapper.deleteBySitePatrolId(ids);
    }


    @Override
    protected void afterGet(WasteDisposalSitePatrol wasteDisposalSitePatrol) {
        //添加消纳场关联属性
        if (wasteDisposalSitePatrol.getSiteId() != null) {
            //查找勘察记录管理所属消纳场
            WasteDisposalSite wasteDisposalSite = wasteDisposalSiteMapper.findById(wasteDisposalSitePatrol.getSiteId());
            //赋值
            wasteDisposalSitePatrol.setWasteDisposalSite(wasteDisposalSite);
        }
        //添加消纳场勘察记录图片关联属性
        if (wasteDisposalSitePatrol.getId() != null) {
            //查找勘察记录管理所属消纳场勘察记录图片
            WasteDisposalSitePatrolPicture wasteDisposalSitePatrolPicture = wasteDisposalSitePatrolPictureMapper.findBySitePatrolId(wasteDisposalSitePatrol.getId());
            //赋值
            wasteDisposalSitePatrol.setWasteDisposalSitePatrolPicture(wasteDisposalSitePatrolPicture);
        }
    }

    @Override
    protected void afterFindAll(List<WasteDisposalSitePatrol> list) {
        for (WasteDisposalSitePatrol wasteDisposalSitePatrol : list) {
            //添加消纳场关联属性
            if (wasteDisposalSitePatrol.getSiteId() != null) {
                //查找勘察记录管理所属消纳场
                WasteDisposalSite wasteDisposalSite = wasteDisposalSiteMapper.findById(wasteDisposalSitePatrol.getSiteId());
                //赋值
                wasteDisposalSitePatrol.setWasteDisposalSite(wasteDisposalSite);
            }
        }
    }

    public void uploadHeadImgFile(Integer id, MultipartFile[] file, String remark) {
        for (MultipartFile files : file) {
            if (files.isEmpty()) {
                throw new MyWebException("请选择要上传的图片");
            } else {
                //图片保存位置
                String filePath = "C:\\img\\";
                //创建文件mulu
                FileUtil.mkdir(filePath);
                //获取文件名
                String fileName = files.getOriginalFilename();
                //获取文件扩展名
                String extensionName = FileUtil.extName(fileName);
//                修改文件名为时间戳
                fileName = String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()) + "." + extensionName;
                //获取文件类型
                String fileType = files.getContentType();
                if (fileType.equals("image/jpeg") || fileType.equals("image/png") || fileType.equals("image/jpg")) {
                    if (files.getSize() / 1000 > 10000) {
                        throw new MyWebException("图片大小不能超过10M,请重新选择要上传的图片");
                    }
                    try {
                        WasteDisposalSitePatrolPicture pic = new WasteDisposalSitePatrolPicture();
                        pic.setPatrolId(id);
                        pic.setPictureUrl(fileName);
                        pic.setRemark(remark);
                        wasteDisposalSitePatrolPictureMapper.save(pic);
                        files.transferTo(new File(filePath + fileName));
                    } catch (IOException e) {
                        throw new MyWebException("上传失败：" + e.getMessage());
                    }
                } else {
                    throw new MyWebException("图片格式错误");
                }
            }
        }
    }
}
